import { toJS } from 'mobx'
import { makeAutoObservable } from 'mobx'
import { nanoid } from 'nanoid'
// model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"

export class TaskStore {
  list = [
    {
      id: nanoid(),
      name: '学习react',
      isDone: true,
      isLock: true,
    },
    {
      id: nanoid(),
      name: '搞定mobx',
      isDone: false,
      isLock: true,
    },
  ]
  constructor() {
    makeAutoObservable(this)
  }

  get taskTotal() {
    return this.list.length
  }

  get taskFinisedCount() {
    return this.list.filter((item) => item.isDone).length
  }

  init(data = []) {
    this.list = data
  }

  addTask(value) {
    let data = {
      id: nanoid(),
      name: value,
      isDone: false,
      isLock: true,
    }

    this.list.push(data)
  }

  getById(id) {
    return this.list.find((item) => item.id === id)
  }

  delTask(id) {
    this.list = this.list.filter((item) => item.id !== id)
  }

  checkTask(id) {
    const item = this.getById(id)
    item.isDone = !item.isDone
  }

  unLockChange(id, isLock) {
    const item = this.getById(id)
      // console.log(toJS(item))
      item.isLock = !isLock
  }

  modifiedTask(id, value) {
    const item = this.getById(id)
    item.name = value
  }
}
